package com.dis.service.controller.admin;

import com.dis.common.annotation.CheckPermission;
import com.dis.pojo.dto.MenuTreeDTO;
import com.dis.pojo.entity.Permission;
import com.dis.service.service.PermissionService;
import com.dis.common.context.BaseContext;
import com.dis.common.result.Result;
import com.dis.pojo.vo.UserPermissionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/permission")
@Api(tags = "权限管理接口")
@Slf4j
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 获取所有权限列表
     */
    @GetMapping("/list")
    @ApiOperation("获取所有权限列表")
    @CheckPermission("permission:query")
    public Result<List<Permission>> getAllPermissions() {
        List<Permission> permissions = permissionService.getAllPermissions();
        return Result.success(permissions);
    }

    /**
     * 获取权限树
     */
    @GetMapping("/tree")
    @ApiOperation("获取权限树")
    @CheckPermission("permission:query")
    public Result<List<MenuTreeDTO>> getPermissionTree() {
        List<MenuTreeDTO> permissionTree = permissionService.getPermissionTree();
        return Result.success(permissionTree);
    }

    /**
     * 根据ID获取权限
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID获取权限")
    @CheckPermission("permission:query")
    public Result<Permission> getPermissionById(@PathVariable Long id) {
        Permission permission = permissionService.getById(id);
        return Result.success(permission);
    }

    /**
     * 新增权限
     */
    @PostMapping
    @ApiOperation("新增权限")
    @CheckPermission("permission:add")
    public Result<Boolean> addPermission(@RequestBody Permission permission) {
        boolean result = permissionService.addPermission(permission);
        return Result.success(result);
    }

    /**
     * 更新权限
     */
    @PutMapping
    @ApiOperation("更新权限")
    @CheckPermission("permission:edit")
    public Result<Boolean> updatePermission(@RequestBody Permission permission) {
        boolean result = permissionService.updatePermission(permission);
        return Result.success(result);
    }

    /**
     * 删除权限
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除权限")
    @CheckPermission("permission:delete")
    public Result<Boolean> deletePermission(@PathVariable Long id) {
        boolean result = permissionService.deletePermission(id);
        return Result.success(result);
    }

    /**
     * 获取用户权限编码列表
     */
    @GetMapping("/codes/{userId}")
    @ApiOperation("获取用户权限编码列表")
    public Result<List<String>> getPermissionCodesByUserId(@PathVariable Long userId) {
        List<String> permissionCodes = permissionService.getPermissionCodesByUserId(userId);
        return Result.success(permissionCodes);
    }

    @GetMapping("/user/menus")
    @ApiOperation("获取当前用户的菜单权限")
    public Result<List<MenuTreeDTO>> getUserMenus() {
        // 从JWT中获取用户ID
        Long userId = BaseContext.getCurrentId();
        System.out.println("获取用户菜单，用户ID: " + userId);
        
        List<MenuTreeDTO> menus = permissionService.getMenuTreeByUserId(userId);
        System.out.println("返回菜单树节点数量: " + (menus != null ? menus.size() : "null"));
        
        return Result.success(menus);
    }

    @GetMapping("/user/permissions")
    @ApiOperation("获取当前用户的API权限")
    public Result<List<String>> getUserPermissions() {
        // 从JWT中获取用户ID
        Long userId = BaseContext.getCurrentId();
        List<String> permissions = permissionService.getPermissionCodesByUserId(userId);
        return Result.success(permissions);
    }

    @GetMapping("/user/info")
    @ApiOperation("获取当前用户的权限信息")
    public Result<UserPermissionVO> getUserPermissionInfo() {
        // 从JWT中获取用户ID
        Long userId = BaseContext.getCurrentId();

        UserPermissionVO vo = new UserPermissionVO();
        // 获取菜单权限
        List<MenuTreeDTO> menus = permissionService.getMenuTreeByUserId(userId);
        vo.setMenus(menus);

        // 获取API权限
        List<String> permissions = permissionService.getPermissionCodesByUserId(userId);
        vo.setPermissions(permissions);

        return Result.success(vo);
    }
}
